﻿using System.Linq;

namespace ComeSeeThis_MVC2.Models
{
    public class VehicleTypeRepository
    {
        private readonly ComeSeeThisDBEntities _entities = new ComeSeeThisDBEntities();

        #region Query Methods

        public IQueryable<VehicleType> GetAllVehicleTypes(bool? approvedOnly = true)
        {
            return _entities.VehicleTypes.Where(x=>x.Approved == approvedOnly).OrderBy(s => s.Scale).ThenBy(c => c.VehicleTypeName);
        }

        public IQueryable<VehicleType> GetAllVehicleTypesByTireId(int tireId, bool? approvedOnly = true)
        {
            var boo = _entities;//.VehicleTypes;
            var boo2 = from vehicleType in boo.VehicleTypes
                       from c in vehicleType.Tires
                       where c.TireId == tireId
                       select vehicleType;
            return boo2;
        }

        //public IQueryable<VehicleType> GetAllVehicleTypesByVehicleTypeId(int vehicleTypeId)
        //{
        //    return _entities.VehicleTypes.Where(c => c.VehicleTypeId == vehicleTypeId);
        //}

        public VehicleType GetVehicleType(int vehicleTypeId)
        {
            return _entities.VehicleTypes.SingleOrDefault(c => c.VehicleTypeId == vehicleTypeId);
        }
        
        #endregion

        #region Insert / Delete Methods

        public void Add(VehicleType vehicleType)
        {
            _entities.VehicleTypes.AddObject(vehicleType);
        }

        public void Delete(VehicleType vehicleType)
        {
            // Delete the vehicleType
            _entities.VehicleTypes.DeleteObject(vehicleType);
        }

        #endregion

        #region Persistance

        public void Save()
        {
            _entities.SaveChanges();
        }

        #endregion

    }
}